package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.common.dto.DocDto;
import com.koron.css2.common.service.AbstractExportDoc;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjInfoQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ConstructionAcceptanceForm extends AbstractExportDoc {

    @Override
    public DocDto getDocText(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            Map<String,Object> param=new HashMap<>();
             //获取参数
            PjInfoQueryBean pjInfoQueryBean = JsonUtils.objectToPojo(req.getData(), PjInfoQueryBean.class);
            //获取工程信息
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PjInfoBean pjInfoBean = pjInfoMapper.getSingle(pjInfoQueryBean);
            //获取客户信息
            CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
            CtmInfoBean ctmInfoBean = ctmMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), 2);
            //获取水表信息
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            List<PjMeter> pjMeterList=pjMeterMapper.getListByConsultId(pjInfoBean.getId());
            String meterBore="";
            if (pjMeterList!=null&&pjMeterList.size()!=0){
                MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
                MeterBoreBean boreBean = meterBoreMapper.selectMeterBoreById(pjMeterList.get(0).getMeterBore());
                meterBore=boreBean == null ?pjMeterList.get(0).getMeterBore():boreBean.getBoreName();
            }
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String dateString = formatter.format(pjInfoBean.getBillDate());
            Integer meterNum= pjMeterMapper.getCount(pjInfoBean.getId());
            param.put("meterBore",meterBore);
            param.put("billNo",pjInfoBean.getBillNo());
            param.put("billDate",dateString);
            param.put("installAddr",pjInfoBean.getInstallAddr());
            param.put("ctmName",ctmInfoBean.getCtmName());
            param.put("mobile",ctmInfoBean.getMobile());
            param.put("meterNum",meterNum);
            DocDto docDto = new DocDto();
            docDto.setTitle("2023年供水查勘施工验收单");
            docDto.setTemplate("2023年供水查勘施工验收单.docx");
            docDto.setParam(param);
            return docDto;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new RuntimeException("获取供水查勘施工验收单内容失败。");
        }
    }
}
